﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using lapinou_game2.GameInteraction;

namespace lapinou_game2.GameObjects
{
    /// <summary>
    /// La classe Unit est le père de tous les objets sur la map
    /// Elle gère donc les collisions par Bounding box
    /// </summary>
    public class Unit
    {
        // Déclaration des variables
        GraphicsDeviceManager graphics;

        private string leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation;
        private float speedDec = 5;

        Collisions collisions;
        Physic physic;



        public Unit(GraphicsDeviceManager Graphics)
        {
            graphics = Graphics;            
        }

        public void Initialize()
        {        
  
        }

        public void Update(ref BoundingSphere UnitToTest, ref Vector2 vitesse)
        {
            Vector2 position = new Vector2(UnitToTest.Center.X, UnitToTest.Center.Y);

            // gestion des collisions (par ref ou autre)
            leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation = collisions.getHitTestValue(UnitToTest);

            if (leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation == "no")
            {                 
                position = physic.Newton2(position, vitesse);
                vitesse.Y -= speedDec;
                if (vitesse.Y < 0)
                    vitesse.Y = 0;
                if (vitesse.X > 0)
                {
                    vitesse.X -= speedDec;
                    if (vitesse.X < 0)
                        vitesse.X = 0;
                }
                else
                {
                    vitesse.X += speedDec;
                    if (vitesse.X > 0)
                        vitesse.X = 0;
                }

            }
            else if (leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation == "top")
            {
                vitesse = Vector2.Zero;
            }
            else if (leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation == "left")
            {
                vitesse = Vector2.Zero;
                position = physic.Newton2(position, vitesse);
            }
            else if (leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation == "right")
            {
                vitesse = Vector2.Zero;
                position = physic.Newton2(position, vitesse);
            }
            else if (leLapinEstIlPoseSurUneBoundingBoxPointDinterrogation == "bellow")
            {
                vitesse.Y = 0f;
                position = physic.Newton2(position, vitesse);
            }

            UnitToTest = new BoundingSphere(new Vector3(position.X, position.Y, 0f), UnitToTest.Radius);           
                      
        }


    }
}
